# -*- coding:utf-8 -*-
import pymongo
from bson.code import Code
import mongoDB
import sys

input = sys.argv[1]
outFile = sys.argv[2]

collections = input
collection = mongoDB.connMongo(collections)

mapper = Code("""function() {
        emit({u:this._id.u}, {count:1});
}
""")

reducer = Code(""" function (key, values) {
    var cnt = 0;
    values.forEach(function(val) { cnt += val.count;});
    return {"count":cnt};
}
""")

result = collection.map_reduce(mapper, reducer, out = outFile, full_response=True, query={})

print result

ret = collection.myresult.find()
print ret
